package com.sf.wisdomkg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.sf.wisdomkg.R;
import com.sf.wisdomkg.utils.MyOnItemClickListener;
import com.sf.wisdomkg.utils.MyOnItemLongClickListener;
import com.sf.wisdomkg.xRefreshView.DensityUtil;

import java.util.ArrayList;
import java.util.HashMap;

public class HomeInfoListAdapter extends BaseRecyclerAdapter<HomeInfoListAdapter.HomeInfoListAdapterHolder> {
    private ArrayList<HashMap<String, String>> arrayList;
    private int largeCardHeight, smallCardHeight;
    private Context context;
    //点击事件的定义
    private MyOnItemClickListener itemClickListener;
    private MyOnItemLongClickListener itemLongClickListener;

    public HomeInfoListAdapter(ArrayList<HashMap<String, String>> list, Context context) {
        this.arrayList = list;
        largeCardHeight = DensityUtil.dip2px(context, 150);
        smallCardHeight = DensityUtil.dip2px(context, 100);
        this.context = context;
    }

    @Override
    public void onBindViewHolder(final HomeInfoListAdapterHolder holder, int position, boolean isItem) {


        holder.list_title.setText(arrayList.get(position).get("name"));
        holder.list_tv.setText(arrayList.get(position).get("content"));
        holder.list_time.setText(arrayList.get(position).get("createTime"));

//        Glide.with(context).load(arrayList.get(position).get("thumb")).into(holder.nongjia_img);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();


        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListener.OnItemClickListener(holder.itemView, holder.getLayoutPosition());
                }
            });
        } /*自定义item的长按事件不为null，设置监听事件*/
        if (itemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    itemLongClickListener.OnItemLongClickListener(holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });
        }

        if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            holder.rootView.getLayoutParams().height = position % 2 != 0 ? largeCardHeight : smallCardHeight;
        }
    }

    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return arrayList.size();
    }

    @Override
    public HomeInfoListAdapterHolder getViewHolder(View view) {
        return new HomeInfoListAdapterHolder(view, false);
    }

    public void setData(ArrayList<HashMap<String, String>> list) {
        this.arrayList = list;
        notifyDataSetChanged();
    }

    @Override
    public HomeInfoListAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.home_info_list_item, parent, false);
        HomeInfoListAdapterHolder vh = new HomeInfoListAdapterHolder(v, true);
        return vh;
    }

//    public void insert(ArrayList<HashMap<String,String>> list, int position) {
//        insert(list, person, position);
//    }

    public void remove(int position) {
        remove(arrayList, position);
    }

    public void clear() {
        clear(arrayList);
    }

    public class HomeInfoListAdapterHolder extends RecyclerView.ViewHolder {

        public View rootView;
        public TextView list_time,list_title,list_tv;

        public HomeInfoListAdapterHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                list_time = (TextView) itemView.findViewById(R.id.list_time);
                list_title = (TextView) itemView.findViewById(R.id.list_title);
                list_tv = (TextView) itemView.findViewById(R.id.list_tv);


            }

        }
    }

    /**
     * 列表点击事件
     *
     * @param itemClickListener
     */
    public void setOnItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 列表长按事件
     *
     * @param itemLongClickListener
     */
    public void setOnItemLongClickListener(MyOnItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }
}